﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CFG = System.Configuration;

namespace AutomatizzazioneCleverConsumit
{
    public partial class Configuration : Form
    {
        private bool isSaved = false;
        public Configuration()
        {
            InitializeComponent();
            LoadConfig();
        }

        public void LoadConfig()
        {
            try
            {
                //email
                senderTB.Text = ConfigUtil.GetEmailSender();
                senderPwTB.Text = ConfigUtil.GetEmailSenderPassword();
                mailServerTB.Text = ConfigUtil.GetEmailServer();
                mailPortTB.Text = ConfigUtil.GetEmailPort();
                receiverTB.Text = ConfigUtil.GetEmailDest();                
                //general
                websiteTB.Text = ConfigUtil.GetWebSite();
                userTB.Text = ConfigUtil.GetWebUser();
                passwordTB.Text = ConfigUtil.GetWebPassword();
                hourCB.SelectedItem = ConfigUtil.GetHours();
                minsCB.SelectedItem = ConfigUtil.GetMinutes();
                intervalCB.SelectedItem = ConfigUtil.GetInterval();
                hideWebCheckBox.Checked = CFG.ConfigurationManager.AppSettings["HideWeb"].ToLower() == "true" ? true : false;
                outputTB.Text = ConfigUtil.GetOutput();
                logTB.Text = ConfigUtil.GetLog();
            }
            catch { }

        }

        private void SaveConfig()
        {

            if (
                senderTB.Text != string.Empty
                && senderPwTB.Text != string.Empty
                && mailServerTB.Text != string.Empty
                && mailPortTB.Text != string.Empty
                && receiverTB.Text != string.Empty
                && websiteTB.Text != string.Empty //general
                && userTB.Text != string.Empty
                && passwordTB.Text != string.Empty
                && hourCB.SelectedItem.ToString() != string.Empty
                && minsCB.SelectedItem.ToString() != string.Empty
                && intervalCB.SelectedItem.ToString() != string.Empty
                && outputTB.Text != string.Empty
                && logTB.Text != string.Empty
                )
            {
                try
                {
                    CFG.Configuration cfg = CFG.ConfigurationManager.OpenExeConfiguration(System.Configuration.ConfigurationUserLevel.None);
                    cfg.AppSettings.Settings["EmailSender"].Value = senderTB.Text;
                    cfg.AppSettings.Settings["EmailSenderPassword"].Value = senderPwTB.Text;
                    cfg.AppSettings.Settings["EmailServer"].Value = mailServerTB.Text;
                    cfg.AppSettings.Settings["EmailPort"].Value = mailPortTB.Text;                    
                    cfg.AppSettings.Settings["EmailDest"].Value = receiverTB.Text;
                    cfg.AppSettings.Settings["WebUser"].Value = userTB.Text;
                    cfg.AppSettings.Settings["WebPAssword"].Value = passwordTB.Text;
                    cfg.AppSettings.Settings["HideWeb"].Value = hideWebCheckBox.Checked == true ? "true" : "false";
                    cfg.AppSettings.Settings["Hours"].Value = hourCB.SelectedItem.ToString();
                    cfg.AppSettings.Settings["Minutes"].Value = minsCB.SelectedItem.ToString();
                    cfg.AppSettings.Settings["Interval"].Value = intervalCB.SelectedItem.ToString();
                    cfg.AppSettings.Settings["Output"].Value = outputTB.Text;
                    cfg.AppSettings.Settings["Log"].Value = logTB.Text;
                    
                    cfg.Save(System.Configuration.ConfigurationSaveMode.Full);
                    CFG.ConfigurationManager.RefreshSection("appSettings");
                    isSaved = true;
                }
                catch (Exception ex)
                {
                    Logger.Log(ex.Message, LogType.Error);
                }
            }
            else
            {
                MessageBox.Show("Per favore compilare tutti i campi!");
            }

        }

        private void saveBTN_Click(object sender, EventArgs e)
        {
            SaveConfig();
        }

        private void cancelBTN_Click(object sender, EventArgs e)
        {
            LoadConfig();
        }

        private void Configuration_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!isSaved)
            {
                DialogResult dr = MessageBox.Show("Configurazione non salvata. Procedere?", "Attenzione!", MessageBoxButtons.OKCancel);
                switch (dr)
                {
                    case DialogResult.Cancel:
                        e.Cancel = true;
                        break;
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DialogResult result = folderBrowserDialog1.ShowDialog();
            if (result == DialogResult.OK)
            {
                outputTB.Text = folderBrowserDialog1.SelectedPath;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            DialogResult result = folderBrowserDialog2.ShowDialog();
            if (result == DialogResult.OK)
            {
                logTB.Text = folderBrowserDialog2.SelectedPath;
            }
        }
    }
}
